package com.ycu.jk16.mall.dao.provider;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.PRODUCTTABLE;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import com.ycu.jk16.mall.domain.Product;

public class ProductDynaSqlProvider {

	public String selectWhitParam(Map<String, Object> params) {
		String sql = new SQL() {
			{
				SELECT("*");
				FROM(PRODUCTTABLE);
				if (params.get("productName") != null) {
					String productName = (String) params.get("productName");
					if (productName != null && !productName.equals("")) {
						WHERE(" name = #{productName}");
					}
				}
				if (params.get("productId") != null) {

					String productId = String.valueOf(((Integer) params.get("productId")).toString());
					if (productId != null && !productId.equals("")) {
						WHERE(" id = #{productId}");
					}

				}
				if (params.get("categoryId") != null) {
					if ((Integer) params.get("categoryId") != -1) {
						String categoryId = String.valueOf(((Integer) params.get("categoryId")).toString());
						if (categoryId != null && !categoryId.equals("")) {
							WHERE(" category_Id = #{categoryId}");
						}
					}

				}
				if (params.get("keyword") != null) {
					String keyword = (String) params.get("keyword");
					if (keyword != null && !keyword.equals("")) {
						WHERE(" name LIKE CONCAT ('%',#{keyword},'%') ");
					}
				}
				if (params.get("orderBy") != null) {
					String orderBy = (String) params.get("orderBy");
					if (orderBy != null && !orderBy.equals("")) {
						String[] item = orderBy.split("_");
						ORDER_BY("ORDER BY #{item[0]} #{item[1]}");
					}
				}
			}
		}.toString();

		// 此处加入了分页限制，每页显示 = (页数-1)*每页显示数, 页数*每页显示数 -1
		if (params.get("pageNum") != null) {
			int pageNum = (Integer) params.get("pageNum");
			int pageSize = (Integer) params.get("pageSize");
			sql += " limit " + (pageNum - 1) * pageSize + "," + (pageNum * pageSize);
		}

		return sql;
	}

	public String insertProduct(Product product) {

		return new SQL() {
			{
				INSERT_INTO(PRODUCTTABLE);
				if (product.getCategory() != null) {
					VALUES("category_Id", "#{category.id}");
				}
				if (product.getParentCategory() != null) {
					VALUES("parentCategory_Id", "#{parentCategory.id}");
				}
				if ((product.getName() != null) && (!product.getName().contentEquals(""))) {
					VALUES("name", "#{name}");
				}
				if ((product.getSubtitle() != null) && (!product.getSubtitle().equals(""))) {
					VALUES("subtitle", "#{subtitle}");
				}
				if (product.getImageHost() != null) {
					VALUES("imagehost", "#{imageHost}");
				}
				if (product.getMainImage() != null) {
					VALUES("mainImage", "#{mainImage}");
				}
				if ((product.getDetail() != null) && (!product.getDetail().equals(""))) {
					VALUES("Detail", "#{detail}");
				}
				if (product.getPrice() != null) {
					VALUES("price", "#{price}");
				}
				if (product.getStock() != null) {
					VALUES("stock", "#{stock}");
				}
				if (product.getStatus() != null) {
					VALUES("status", "#{status}");
				}
				if (product.getCreateTime() != null) {
					VALUES("create_time", "#{createTime}");
				}
				if (product.getUpdateTime() != null) {
					VALUES("update_time", "#{updateTime}");
				}
			}
		}.toString();
	}

	public String updateProduct(Product product) {

		return new SQL() {
			{
				UPDATE(PRODUCTTABLE);
				if (product.getId() != null) {
					if (product.getCategory() != null) {
						SET(" category_Id = #{category.id} ");
					}
					if (product.getParentCategory() != null) {
						SET(" parentCategory_Id = #{parentCategory.id} ");
					}
					if ((product.getName() != null) && (!product.getName().equals(""))) {
						SET(" name = #{name} ");
					}
					if ((product.getSubtitle() != null) && (!product.getSubtitle().equals(""))) {
						SET(" subtitle = #{subtitle} ");
					}
					if (product.getMainImage() != null) {
						SET(" mainImage = #{mainImage} ");
					}
					if ((product.getDetail() != null) && (!product.getDetail().equals(""))) {
						SET(" detail = #{detail} ");
					}
					if (product.getPrice() != null) {
						SET(" price = #{price} ");
					}
					if (product.getStock() != null) {
						SET(" stock = #{stock} ");
					}
					if (product.getStatus() != null) {
						SET(" status = #{status} ");
					}
					if (product.getUpdateTime() != null) {
						SET(" update_time = #{updateTime} ");
					}
				}
				WHERE(" id = #{id} ");
			}
		}.toString();
	}
}
